package com.platform.example.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.PageUtils;
import com.platform.example.dao.CompanyRepository;
import com.platform.example.dao.PersonRepository;
import com.platform.example.entity.Company;
import com.platform.example.entity.Person;
import com.platform.example.service.PersonService;
import com.platform.framework.controller.BaseController;


/**
 * 
  * demo controller 	
  * @author 王求卿[wangqiuqing] Tel:18101037995
  * @createDate 2017年8月8日 上午8:44:24
 */
@RestController
@RequestMapping("/example/person")
public class PersonController extends BaseController{
	
	@Autowired
	PersonService personService;
	
	@Autowired
	CompanyRepository companyRepository;
	
	@Autowired
	PersonRepository personRepository;
	
	/**
	 * http://127.0.0.1:7777/cbms/example/person/json?companyId=1
	  * demo  分页miniui集合	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @retu
	  * @throws Exception
	  * @createDate 2017年8月8日 上午8:44:56
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "json", method = { RequestMethod.GET })
	public String getJson(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("companyId");//根据companyId查询对应的json信息
		Company company = companyRepository.findOne(searchCtx);
		return JSONObject.toJSONString(company);
	}
	
	/**
	 *  http://127.0.0.1:7777/cbms/example/person/json1?companyName=中公
	  * demo  分页miniui集合	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @throws Exception
	  * @createDate 2017年8月8日 上午8:44:56
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "json1", method = { RequestMethod.GET })
	public String getJson1(HttpServletRequest request) throws Exception {
		String companyName = request.getParameter("companyName");//根据companyId查询对应的json信息
		List<Person> persons = personRepository.findByCompany_NameLike("%"+companyName+"%");
		return JsonUtils.dealJson(persons);
	}
	
	
	/**
	 * 级联插入
	 *  http://127.0.0.1:7777/cbms/example/person/save
     * 添加订单Order时同时添加两个订单项OrderItem，因为订单Order为双向关系维护者，起着主导作用
     */
	@Transactional
	@RequestMapping(value = "save", method = { RequestMethod.GET })
    public String save() {
		
		Company pany = new Company(this.getUUID(),"王求卿","地锦路","18101037995");
		
		pany.addPerson(new Person(this.getUUID(),18,"LLL"));
		pany.addPerson(new Person(this.getUUID(),19,"JJJ"));
		
		companyRepository.save(pany);
		
		
		return "success";
    }
    
	
	
	
	/**
	 * 
	  * demo  分页miniui集合	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @throws Exception
	  * @createDate 2017年8月8日 上午8:44:56
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "pagelist", method = { RequestMethod.POST })
	public String personPageList(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("searchPhrase");//查询条件框
		Pageable pageable = null;
		try{
			pageable = PageUtils.dealminiRequest(request);//两种，还有一种是提供bootstrap的处理
		}catch (Exception e) {
			e.printStackTrace();
		}
		Page<Person> pages= personService.findPersonPage(searchCtx, pageable);
		return JsonUtils.dealMiniPage(pages);//两种，一种是返回miniui需要的格式，一种是返回bootstrap需要的格式
	}
	
	/**
	 * 
	  * demo add	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @createDate 2017年8月8日 上午8:45:34
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	//@PreAuthorize("hasAuthority('person:add')")
	@RequestMapping(value = "add", method = { RequestMethod.POST })
	public String personSave(HttpServletRequest request){
		String data = request.getParameter("data");
		Person person =  JSONObject.parseObject(data, Person.class);
		return personService.savePerson(person);
	}
	

	/**
	 * 
	  * demo update 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @createDate 2017年8月8日 上午8:45:45
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	//@PreAuthorize("hasAuthority('person:update')")
	@RequestMapping(value = "update", method = { RequestMethod.POST })
	public String personUpdate(HttpServletRequest request){
		String data = request.getParameter("data");
		Person person =  JSONObject.parseObject(data, Person.class);
		return personService.updatePerson(person);
	}
	
	/**
	 * 
	  * demo  delete 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	  * @createDate 2017年8月8日 上午8:45:53
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	//@PreAuthorize("hasAuthority('person:delete')")
	@RequestMapping(value = "delete", method = { RequestMethod.DELETE })
	public String personDelete(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return personService.deletePerson(ids);
	}
	
	/**
	 * 
	  * demo 主界面
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @return
	  * @createDate 2017年8月8日 上午8:46:04
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
//	@PreAuthorize("hasAuthority('person')")//有用户添加权限的才能访问    
    @RequestMapping("/index")  
    public ModelAndView personIndex() {
    	ModelAndView mv = new ModelAndView("example/person/index");
		return mv;
    }
	

	/**
	 * 
	  * demo 维护界面 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @return
	  * @createDate 2017年8月8日 上午8:46:20
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
    @RequestMapping("/edit")  
    public ModelAndView personAdd(){
    	ModelAndView mv = new ModelAndView("example/person/edit");
		return mv;
    }
    
    

	


}